<template>
  <a-modal v-model:visible="visible" :footer="null" :title="$t('menu.mo:manufactureorder:edit')+$t('hangManage.road')" width="90%" wrap-class-name="large-modal" :maskClosable="false" :confirm-loading="loading" :keyboard="false">
    <a-spin :spinning="loading">
      <div class="route-chart-modal-container">
        <aside>
          <a-row class="moinfo-box pb-20">
            <a-col :span="24" class="mb-8">
              <span>{{$t('hangManage.makeNo')}}：</span>
              <span>{{moInfo.moNo}} </span>
            </a-col>
            <a-col :span="24" class="mb-8">
              <span>{{$t('basicData.styleNo')}}：</span>
              <span>{{moInfo.styleNo}} </span>
            </a-col>
            <a-col :span="24">
              <span> {{$t('basicData.styleNa') }}：</span>
              <span>{{moInfo.styleName}} </span>
            </a-col>
          </a-row>
          <div class="version-box">
            <a-form :model="formState" ref="formRef" layout="vertical">
              <a-form-item :label="$t('other.versionName')" :rules="routeRule.name" name="name">
                <a-input v-model:value.trim="formState.name" :maxLength="19" :placeholder="$t('common.pleaseInput')" />
              </a-form-item>
              <a-form-item :label="$t('basicData.section')" name="section" :rules="routeRule.section">
                <a-select :placeholder="$t('common.pleaseSelect')" allowClear v-model:value="formState.section">
                  <a-select-option v-for="(item, index) in [$t('components.dg')]" :key="index" :value="item">{{item }}</a-select-option>
                </a-select>
              </a-form-item>
              <a-form-item :label="$t('hangManage.startProcess')" name="firstProcess">
                <a-select :placeholder="$t('common.pleaseSelect')" allowClear v-model:value="formState.firstProcess">
                  <a-select-option v-for="(item, index) in processOption" :key="index" :value="item.id">{{item.name}}</a-select-option>
                </a-select>
              </a-form-item>
              <a-form-item :label="$t('hangManage.endProcess')" name="lastProcess">
                <a-select :placeholder="$t('common.pleaseSelect')" allowClear v-model:value="formState.lastProcess">
                  <a-select-option v-for="(item, index) in processOption"  :key="index" :value="item.id">{{item.name}}</a-select-option>
                </a-select>
              </a-form-item>
            </a-form>
          </div>
        </aside>
        <section>
          <RoadmapList v-if="visible" ref="roadmapListRef" :lineStationLists="lineStationLists" :dataAll="dataAll" :roueteVersionId="roueteVersionId" :moId="moId" :moInfo="moInfo" operateType="add" @onProcessInfo="onProcessInfo" />
        </section>
      </div>
      <div class="modal-operate-btn">
        <a-button class="cancel" @click="onCancle">{{$t('common.cancel')}}</a-button>
        <a-button type="primary" class="confirm" @click="onConfirm">{{$t('common.ok')}}</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>
<script setup>
import { ref } from 'vue'
import RoadmapList from './roadmapList'
import { routeRule } from './routeChartConfig'
import { message } from 'ant-design-vue'
import axios from "@/utils/request"
import {cloneDeep, isEmpty} from 'lodash'
import i18n from '@/locales/index' 
const { t } = i18n.global
let visible = ref(false)
const loading = ref(false)
let moId = ref(null)  // 制单id
let moInfo = ref({})
let roueteVersionId = ref(null)
let roadmapListRef = ref()
let dataAll = ref() // 返回数据整体
const formState = ref({})
let formRef = ref()
let processOption = ref([])
let lineStationLists = ref([])
const emits = defineEmits(['ok'])
const open = async (record,lineStationList) => {
  visible.value = true
  loading.value = true
  lineStationLists.value = lineStationList
  console.log(lineStationLists.value)
  formState.value = { section: t('components.dg')}
  try {
    console.log(record,'-----------------------------------')
    doParams(record)

    await getMakeInfo(moId.value)
    loading.value = false
  } catch {
    loading.value = false
  }
}

// 处理数据
const doParams = (record) => {
  if (!isEmpty(record)){
    dataAll.value = record
  }
  if (!isEmpty(record.ieRoadmapProcRoadmapVO.moId)){
    moId.value = record.ieRoadmapProcRoadmapVO.moId
  }
  if (!isEmpty(record.ieRoadmapVO.ieRoadmapProcVO.detailList)){
    processOption.value = record.ieRoadmapVO.ieRoadmapProcVO.detailList
    console.log(processOption.value)
  }
  if (!isEmpty(record.ieRoadmapProcRoadmapVO.firstProcess) || !isEmpty(record.ieRoadmapProcRoadmapVO.lastProcess)  ){
    formState.value.firstProcess = record.ieRoadmapProcRoadmapVO.firstProcess
    formState.value.lastProcess = record.ieRoadmapProcRoadmapVO.lastProcess
  }else{
    formState.value.firstProcess = undefined
    formState.value.lastProcess = undefined
  }
}

const onProcessInfo = (data, isDelete) => {
  processOption.value = data
  if (isDelete) {
    formState.value.firstProcess = undefined
    formState.value.lastProcess = undefined
  }
}
// 制单信息
const getMakeInfo = async (id) => {
  try {
    const res = await axios.getAction('/api/mo/mo/get', { id })
    moInfo.value = res.data || {}
  } catch {
    message.warning(t('components.getDataErrorPleaseHolder'))
    moInfo.value = {}
  }
}
// const queryTableParams = (tableList) => {
//   let hangerLineList = []
//   tableList.forEach((item, index) => {
//     item.sort = item.sort || index + 1
//     item.forwardIntegration = item.forwardMerge ? 1 : 0
//     item.enable = item.effect ? 1 : 0
//     item.moId = item.moId || moId.value
//     item.moProcRoadmapProcStationList.forEach((el, i) => {
//       el.sort = i + 1
//       el.mainAuxiliaryStation = el.mainStationBoolean ? 2 : 1
//       el.receiveHanger = el.receiveHangerBoolean ? 1 : 0
//       el.moId = el.moId || moId.value
//       hangerLineList.push(el.hangerLineId)
//     })
//   })
//   return {
//     tableList,
//     hangerLineList: Array.from(new Set(hangerLineList))
//   }
// }
const onCancle = () => {
  roadmapListRef.value.tableList = []
  roadmapListRef.value.processId = null
  visible.value = false
  moInfo.value = {}
  roueteVersionId.value = null
  moId.value = null
}
const onConfirm = async () => {
  try {
    loading.value = true
    await formRef.value.validateFields()
    let params = queryParams()
    // console.log(params)
    const res = await axios.postAction('/api/mo/mo/commit', params)
    if (res.success) {
      message.success(res.msg || t('common.doSuccess'))
      loading.value = false
      emits('ok')
      onCancle()
    }
    loading.value = false
  } catch {
    loading.value = false
  }
}

const queryParams = () => {
  let { tableList,dataAll } = roadmapListRef.value
  // console.log(tableList)
  // console.log(dataAll)
  let params = Object.assign({}, formState.value)
  let tableData = queryTableParams1(cloneDeep(tableList))
  params = cloneDeep(dataAll)
  params.ieRoadmapProcRoadmapVO.name = formState.value.name
  params.ieRoadmapProcRoadmapVO.section = formState.value.section
  params.ieRoadmapProcRoadmapVO.firstProcess = formState.value.firstProcess
  params.ieRoadmapProcRoadmapVO.lastProcess = formState.value.lastProcess
  params.ieRoadmapProcRoadmapVO.moProcRoadmapProcList = tableData || []
  params.createType = 'AI排产'
  return params
}
const queryTableParams1 = (tableList) => {
  tableList.forEach((item, index) => {
    item.sort = item.sort || index + 1
    item.forwardIntegration = item.forwardIntegration ? 1 : 0
    item.enable = item.enable ? 1 : 0
    item.moId = item.moId || moId.value
    item.moProcRoadmapProcStationList.forEach((el, i) => {
      el.sort = i + 1
      el.mainAuxiliaryStation = el.mainStationBoolean ? 2 : 1
      el.receiveHanger = el.receiveHangerBoolean ? 1 : 0
      el.moId = el.moId || moId.value
      el.type = el.type ? el.type: 0
      // hangerLineList.push(el.hangerLineId)
    })
  })
  return  tableList
}
defineExpose({
  open
})
</script>